<?php
class QPoDetail
{	
	public function __construct($p_strCountry)
	{
		$this->_country	= $p_strCountry;
		$this->_table	= $this->_country.'_podetails';
	}
	
	public function getbyId($id)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' WHERE id=' . intval($id) . ';';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function getAll()
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' Order by sku;';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function deletebyId($id)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'DELETE FROM '. $this->_table . ' WHERE id=' . intval($id) . ';';
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	public function getbySupplierAndSKU($supplierId, $sku)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '. $this->_table . ' WHERE sku="' .
													 $sku . '" And supplier_id =' . intval($supplierId). ';';
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function getbySupplier($intOffset, $intLimit, $supplierId)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT d.id, d.supplier_id, d.sku, f.product_name, d.cost_item, d.quantity, d.item_tax_amount
		 FROM '. $this->_table . ' d inner join ' . $this->_country . '_forecasts f 
		 ON f.sku = d.sku 
		 WHERE d.supplier_id = ' . intval($supplierId). ' '
			. ' Limit '. $intOffset . ',' . $intLimit ;
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function getTotalBySupplier($supplierId)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT count(1) as total FROM '. $this->_table . ' WHERE supplier_id = ' . intval($supplierId). '';
		$arrResult = $db->fetchAll($strQuery);
		$db->closeConnection();
		return isset($arrResult[0]['total']) ? $arrResult[0]['total'] : 0;
	}
	
	public function insert($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue =$db->insert($this->_table , $p_arrParams);
		
		$db->closeConnection();
		return $resultValue;
	}
	
	public function update($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->update($this->_table, $p_arrParams, "id=" . "'" .$p_arrParams["id"] . "'");
		$db->closeConnection();
		return $resultValue;
	}
	
	public function truncate()
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->execute('TRUNCATE TABLE ' . $this->_table . ';');
		$db->closeConnection();
		return $resultValue;
	}
	
	public function clone_table($date, $p_intAdminId)
	{
		$strTable	= $this->_country.'_podetail_'. $date;
		$strQuery	= '
			CREATE TABLE '.$strTable.' LIKE '.$this->_table.';
			INSERT '.$strTable.' SELECT * FROM '.$this->_table.';';
			
		$db			= Zend_Registry::get('db');
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	public function getDataForPODetailAll()
	{
		$strQuery	=  'Select  supplier_id, sku, f.final_quantity
						From '. $this->_country . '_forecasts f
						Where f.final_quantity > 0 ' ;
		
		$db			= Zend_Registry::get('db');
		$arrResult	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return $arrResult;
	}
	
	public function getDataForPODetailbyForeCastsId($id)
	{
		$strQuery	=  'Select  supplier_id, sku, final_quantity
						From '. $this->_country . '_forecasts
						Where final_quantity > 0  And id =' .intval($id) .';' ;
		
		$db			= Zend_Registry::get('db');
		$arrResult	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return $arrResult;
	}
	
	public function insert_Archives()
	{
		$strQuery	= 'Delete from '. $this->_table . '_archives where DATE_FORMAT(created_at, "%Y-%m-%d")  =
		DATE_FORMAT(now(), "%Y-%m-%d") ;

		insert into '. $this->_table .'_archives 
		(id, supplier_id, sku, cost_item, quantity, item_tax_amount, created_at)
		 Select *, now() as created_at from ' . $this->_table . ' ; ';
		
		$db			= Zend_Registry::get('db');
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	
}
?>